package test;

import net.NetClient;
import net.protocol.MsgArg;
import net.protocol.NetMsgArg;
import net.protocol.NetMsgCenter;
import protocol.HelloWorld;
import world.WorldEventManager;
import net.NetServer;

public class Application {

    public static volatile boolean enabled  = true;

    public static void main(String[] args) throws InterruptedException {



    }

    public void run1() throws InterruptedException {
        NetServer s = new NetServer(54321);
        s.setAcceptHandler(Application::handler);
        s.start();
        NetMsgCenter.getInstance().addMsgListener(HelloWorld.class, art->{
            System.out.println("recv "+ art.content());
        });

        while (enabled)
        {
            WorldEventManager.getInstance().update();
            Thread.sleep(100);
        }
    }

    public static void handler(NetClient client)
    {
        System.out.println("client connected!");
        NetMsgCenter.getInstance().register(client);
        NetMsgArg arg = new NetMsgArg();
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setHi("Server!");
        arg.content(helloWorld);
        arg.client(client);
        NetMsgCenter.getInstance().sendAsync(arg);
    }

    public static void recv(MsgArg arg)
    {
        System.out.println("recv "+ arg);
        HelloWorld msg = arg.content();
        System.out.println(msg);
    }
}
